﻿//-----------------------------------------------------------------------
// <copyright file="Assert.cs" company="tuliprules.org">
//     Copyright (c) 2010 tuplelister, tuliprules.org.
// </copyright>
// <summary>This is the Assert class.</summary>
//-----------------------------------------------------------------------

namespace Tulip.Engine.Archive.EngineOperation
{
    using System.Diagnostics;
    using Tuples;

    /// <summary>
    /// Represents and 'assert' engine operation.   Asserts a single fact to the engine.
    /// </summary>
    internal class Assert : EngineOperation<object>
    {
        /// <summary>
        /// Initializes a new instance of the Assert class.
        /// </summary>
        /// <param name="session">The current tuple store session.</param>
        /// <param name="fact">A fact to be asserted.</param>
        public Assert(TupleStoreSession session, object fact)
        {
            Debug.Assert(fact != null, "Fact to be asserted cannot be null");

            this.session = session;
            this.data = fact;
        }

        #region IEngineOperation Members

        /// <summary>
        /// Performs the engine operation.
        /// </summary>
        /// <param name="facts">Ignored in this implementation.</param>
        /// <returns>Indicates if the engine should start a new match-resolve-do cycle.</returns>
        public override bool Perform(ref object[] facts)
        {
            var tupleList = new TupleMonadLinkedList();

            // Assert the fact and register an entity tuple
            this.session.Assert(this.data);

            // Retrieve the entity tuple from the tupleStore and add to inner list
            tupleList.Add(GetEntityTuple(this.data));

            // Lift the tuple list into the TupleList monad, and add the session for context
            var blm = new TupleList(tupleList, this.session);

            // Add callback to EvaluateType
            blm.AddMonadicFunction(this.session.InitialMonadicFunctions);

            // Start the execution of a computational sequence
            blm.ExecuteSequence();

            return false;
        }

        #endregion
    }
}